package com.cloudlinkscm.base.authcenter.controller;

import com.cloudlinkscm.base.authcenter.model.dto.*;
import com.cloudlinkscm.base.authcenter.service.AuthcRemoteService;
import com.cloudlinkscm.base.authcenter.service.AuthcService;
import com.cloudlinkscm.loms.framework.core.pojo.RestfulApiResponse;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * Created by sin on 2017/7/27.
 *
 * 账户：授权
 */
@RestController
@RequestMapping(AuthcRemoteService.PATH)
public class AuthcController implements AuthcRemoteService {

    @Autowired
    private AuthcService service;

    @ApiOperation(value = "账户授权", notes = "根据账户名跟密码 授权，获取一个新的 accessToken")
    @Override
    public RestfulApiResponse<AccessToken> authenticate(@RequestBody AuthenticationInfo info) {
        return RestfulApiResponse.success(service.doAuthenticate(info));
    }

	@ApiOperation(value = "账户授权 web", notes = "根据账户名跟密码 授权，返回 accessToken 和登录后返回用户的资源")
	@Override
	public RestfulApiResponse<AuthenticationWebInfo> authenticateWeb(AuthenticationInfo info) {
		return RestfulApiResponse.success(service.doAuthenticateWeb(info));
	}

	@ApiOperation(value = "获取账户信息", notes = "根据授权后的 token，获取 cache 的用户信息")
	@Override
	public RestfulApiResponse<UserAccountDTO> account(@PathVariable("token") String token) {
		return RestfulApiResponse.success(service.getAccount(token));
	}

	@ApiOperation(value = "获取账户资源", notes = "根据授权后的 token，获取用户的 resource")
	@Override
	public RestfulApiResponse<List<ResourceTree>> accountResource(@PathVariable("token") String token) {
		return RestfulApiResponse.success(service.accountResource(token));
	}
}
